package com.googlecode.imanager.client.util;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public enum Anchors {
    
    LEFT_TOP            (true, true, false, false),
    TOP_RIGHT           (false, true, true, false),
    RIGHT_BOTTOM        (false, false, true, true),
    BOTTOM_LEFT         (true, false, false, true),

    MIDDLE              (false, false, false, false),
    HORIZONTAL_TOP      (true, true, true, false),
    HORIZONTAL_CENTER   (true, false, true, false),
    HORIZONTAL_BOTTOM   (true, false, true, true),

    VERTICAL_LEFT       (true, true, false, true),
    VERTICAL_CENTER     (false, true, false, true),
    VERTICAL_RIGHT      (false, true, true, true),
    STRETCH             (true, true, true, true),

    CENTER_LEFT         (true, false, false, false),
    CENTER_TOP          (false, true, false, false),
    CENTER_RIGHT        (false, false, true, false),
    CENTER_BOTTOM       (false, false, false, true);

    public static final Anchors DEFAULT = LEFT_TOP;
    
    private static final int LEFT_FLAG = 1;
    private static final int TOP_FLAG = 2;
    private static final int RIGHT_FLAG = 4;
    private static final int BOTTOM_FLAG = 8;
    
    private static Anchors[] cache = new Anchors[0x10];
    
    private final int flags;
    
    private Anchors(boolean left, boolean top, boolean right, boolean bottom) {
        this.flags = (left ? LEFT_FLAG : 0)
                   | (top ? TOP_FLAG : 0)
                   | (right ? RIGHT_FLAG : 0)
                   | (bottom ? BOTTOM_FLAG : 0);
    }

    public static Anchors valueOf(boolean left, boolean top, boolean right, boolean bottom){
        int flags = (left ? LEFT_FLAG : 0)
                  | (top ? TOP_FLAG : 0)
                  | (right ? RIGHT_FLAG : 0)
                  | (bottom ? BOTTOM_FLAG : 0);
        return cache[flags];
    }
    
    public static Anchors parseFlags(int flags){
        return cache[flags & 0xf];
    }
    
    public boolean isLeft() {
        return (flags & LEFT_FLAG) != 0;
    }

    public boolean isTop() {
        return (flags & TOP_FLAG) != 0;
    }

    public boolean isRight() {
        return (flags & RIGHT_FLAG) != 0;
    }

    public boolean isBottom() {
        return (flags & BOTTOM_FLAG) != 0;
    }

    public int getFlags(){
        return flags;
    }
    
    static {
        for (Anchors anchors : values()){
            cache[anchors.flags] = anchors;
        }
    }

    @Override
    public String toString() {
        StringBuilder data = new StringBuilder();
        data.append("Anchors[");
        boolean first = true;

        for (int i=0; i<4; i++){
            if ((flags & (1 << i)) != 0){
                if (first){
                    first = false;
                } else {
                    data.append(", ");
                }
                switch (i) {
                    case 0:
                        data.append("LEFT");
                        break;

                    case 1:
                        data.append("TOP");
                        break;

                    case 2:
                        data.append("RIGHT");
                        break;

                    case 3:
                        data.append("BOTTOM");
                        break;
                }
            }
        }
        
        return data.append("]").toString();
    }

}
